package org.taobao.ibatiside.ui.wizard;

import org.taobao.ibatiside.ui.util.MiscHelper;
import org.taobao.ibatiside.ui.wizard.BatisSettingModelArgument.BatisSettingModelArgumentResult;
import org.taobao.ibatiside.ui.wizard.BatisSettingModelArgument._Field_Type;
import org.apache.commons.lang.*;

public class BatisSettingModelGen
{
  protected static String nl;
  public static synchronized BatisSettingModelGen create(String lineSeparator)
  {
    nl = lineSeparator;
    BatisSettingModelGen result = new BatisSettingModelGen();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "package org.taobao.ibatiside.ui.wizard;" + NL + "" + NL + "import java.util.LinkedList;" + NL + "import java.util.HashMap;";
  protected final String TEXT_2 = NL + "import ";
  protected final String TEXT_3 = ";";
  protected final String TEXT_4 = NL + "public final class BatisSettingModel {" + NL + "\t";
  protected final String TEXT_5 = NL + "\t/**" + NL + "\t*@generated" + NL + "\t*/" + NL + "\tpublic static final String ";
  protected final String TEXT_6 = " = \"";
  protected final String TEXT_7 = "\";" + NL + "\t" + NL + "\t/**" + NL + "\t*@generated" + NL + "\t*/" + NL + "\tprivate ";
  protected final String TEXT_8 = " ";
  protected final String TEXT_9 = ";" + NL + "\t" + NL + "\t/**" + NL + "\t*@generated" + NL + "\t*/" + NL + "\tpublic void set";
  protected final String TEXT_10 = "(";
  protected final String TEXT_11 = " value){" + NL + "\t\t";
  protected final String TEXT_12 = " " + NL + "\t\tif (this.";
  protected final String TEXT_13 = " != null) this.";
  protected final String TEXT_14 = ".clear();" + NL + "\t\t";
  protected final String TEXT_15 = NL + "\t\tif (this.";
  protected final String TEXT_16 = " != null) this.";
  protected final String TEXT_17 = ".eAdapters().clear();" + NL + "\t\t";
  protected final String TEXT_18 = NL + "\t\tObject old_value = this.";
  protected final String TEXT_19 = ";" + NL + "\t\t";
  protected final String TEXT_20 = NL + "\t\tthis.";
  protected final String TEXT_21 = " = value;" + NL + "\t\t";
  protected final String TEXT_22 = NL + "\t\tif (this.";
  protected final String TEXT_23 = " != null) addEMFListener(this.";
  protected final String TEXT_24 = ");" + NL + "\t\t";
  protected final String TEXT_25 = NL + "\t\tchangeSupport.firePropertyChange(";
  protected final String TEXT_26 = ", ";
  protected final String TEXT_27 = ", this.";
  protected final String TEXT_28 = ");" + NL + "\t}" + NL + "\t/**" + NL + "\t*@generated" + NL + "\t*/" + NL + "\tpublic ";
  protected final String TEXT_29 = " get";
  protected final String TEXT_30 = "(){" + NL + "\t\t";
  protected final String TEXT_31 = NL + "\t\tif(";
  protected final String TEXT_32 = " == null){" + NL + "\t\t  ";
  protected final String TEXT_33 = " = new ";
  protected final String TEXT_34 = "();" + NL + "\t\t}" + NL + "\t\t";
  protected final String TEXT_35 = NL + "\t\treturn this.";
  protected final String TEXT_36 = ";" + NL + "\t}" + NL + "\t";
  protected final String TEXT_37 = NL + "\t/**" + NL + "\t*@generated" + NL + "\t*/" + NL + "\tpublic static final String ";
  protected final String TEXT_38 = "_ADD = \"";
  protected final String TEXT_39 = ".insert\";" + NL + "\t/**" + NL + "\t*@generated" + NL + "\t*/" + NL + "\tpublic static final String ";
  protected final String TEXT_40 = "_REMOVE = \"";
  protected final String TEXT_41 = ".remove\";" + NL + "\t";
  protected final String TEXT_42 = NL + "\t/**" + NL + "\t*@generated" + NL + "\t*/" + NL + "\tpublic void ";
  protected final String TEXT_43 = "Insert(";
  protected final String TEXT_44 = " value){" + NL + "\t\t";
  protected final String TEXT_45 = ".add(value);" + NL + "\t\tchangeSupport.firePropertyChange(";
  protected final String TEXT_46 = "_ADD,null,value);" + NL + "\t}" + NL + "\t/**" + NL + "\t*@generated" + NL + "\t*/" + NL + "\tpublic void ";
  protected final String TEXT_47 = "Remove(";
  protected final String TEXT_48 = " value){" + NL + "\t ";
  protected final String TEXT_49 = ".remove(value);" + NL + "\t changeSupport.firePropertyChange(";
  protected final String TEXT_50 = "_REMOVE,value,null);" + NL + "\t}" + NL + "\t";
  protected final String TEXT_51 = NL + "\t/**" + NL + "\t*@generated" + NL + "\t*/" + NL + "\tpublic void ";
  protected final String TEXT_52 = "Insert(";
  protected final String TEXT_53 = " key, ";
  protected final String TEXT_54 = " value){" + NL + "\t\t";
  protected final String TEXT_55 = ".push(key,value);" + NL + "\t\tchangeSupport.firePropertyChange(";
  protected final String TEXT_56 = "_ADD,null,key);" + NL + "\t}" + NL + "\t/**" + NL + "\t*@generated" + NL + "\t*/" + NL + "\tpublic void ";
  protected final String TEXT_57 = "Remove(";
  protected final String TEXT_58 = " key){" + NL + "\t\t";
  protected final String TEXT_59 = ".remove(key);" + NL + "\t\tchangeSupport.firePropertyChange(";
  protected final String TEXT_60 = "_REMOVE,key,null);" + NL + "\t}" + NL + "\t";
  protected final String TEXT_61 = NL + "}";

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    
	BatisSettingModelArgumentResult model = (BatisSettingModelArgumentResult)argument;

    stringBuffer.append(TEXT_1);
    
 for (String one_import : model.getImports()){

    stringBuffer.append(TEXT_2);
    stringBuffer.append(one_import);
    stringBuffer.append(TEXT_3);
    
 }

    stringBuffer.append(TEXT_4);
    
	for (_Field_Type ft : model.getFields())
	{
		String sn = (ft.category==1 || ft.category==4) ? ft.type : ft.category==2? ft.type+"<"+ft.slot1+">" : ft.type+"<"+ft.slot1+","+ft.slot2+">";
		String property_name = "K_"+MiscHelper.getConstName(ft.name);
	
    stringBuffer.append(TEXT_5);
    stringBuffer.append(property_name);
    stringBuffer.append(TEXT_6);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_7);
    stringBuffer.append(sn);
    stringBuffer.append(TEXT_8);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_9);
    stringBuffer.append(StringUtils.capitalize(ft.name));
    stringBuffer.append(TEXT_10);
    stringBuffer.append(sn);
    stringBuffer.append(TEXT_11);
    if (ft.category==2 || ft.category==3){
    stringBuffer.append(TEXT_12);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_13);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_14);
    }else{ if (ft.category==4) {
    stringBuffer.append(TEXT_15);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_16);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_17);
    }
    stringBuffer.append(TEXT_18);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_19);
    }
    stringBuffer.append(TEXT_20);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_21);
    if (ft.category==4) {
    stringBuffer.append(TEXT_22);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_23);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_24);
    }
    stringBuffer.append(TEXT_25);
    stringBuffer.append(property_name);
    stringBuffer.append(TEXT_26);
    stringBuffer.append(ft.category==2 || ft.category==3? "null": "old_value");
    stringBuffer.append(TEXT_27);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_28);
    stringBuffer.append(sn);
    stringBuffer.append(TEXT_29);
    stringBuffer.append(StringUtils.capitalize(ft.name));
    stringBuffer.append(TEXT_30);
    if (ft.category==2 || ft.category==3){
    stringBuffer.append(TEXT_31);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_32);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_33);
    stringBuffer.append(ft.category==2? "LinkedList<"+ft.slot1+">" : "HashMap<"+ft.slot1+", "+ft.slot2+">");
    stringBuffer.append(TEXT_34);
    }
    stringBuffer.append(TEXT_35);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_36);
    if (ft.category==2 || ft.category == 3){
    stringBuffer.append(TEXT_37);
    stringBuffer.append(property_name);
    stringBuffer.append(TEXT_38);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_39);
    stringBuffer.append(property_name);
    stringBuffer.append(TEXT_40);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_41);
    }if (ft.category==2){
    stringBuffer.append(TEXT_42);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_43);
    stringBuffer.append(ft.slot1);
    stringBuffer.append(TEXT_44);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_45);
    stringBuffer.append(property_name);
    stringBuffer.append(TEXT_46);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_47);
    stringBuffer.append(ft.slot1);
    stringBuffer.append(TEXT_48);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_49);
    stringBuffer.append(property_name);
    stringBuffer.append(TEXT_50);
    }else if (ft.category==3){
    stringBuffer.append(TEXT_51);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_52);
    stringBuffer.append(ft.slot1);
    stringBuffer.append(TEXT_53);
    stringBuffer.append(ft.slot2);
    stringBuffer.append(TEXT_54);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_55);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_56);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_57);
    stringBuffer.append(ft.slot1);
    stringBuffer.append(TEXT_58);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_59);
    stringBuffer.append(ft.name);
    stringBuffer.append(TEXT_60);
    }}
    stringBuffer.append(TEXT_61);
    return stringBuffer.toString();
  }
}
